package com.ruoyi.project.channel.freezeInfo.controller;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.channel.businessCashChange.service.IBusinessCashChangeService;
import com.ruoyi.project.channel.businessInfo.domain.BusinessInfo;
import com.ruoyi.project.channel.businessInfo.service.IBusinessInfoService;
import com.ruoyi.project.channel.investConfig.domain.InvestConfig;
import com.ruoyi.project.channel.investConfig.service.IInvestConfigService;
import com.ruoyi.project.channel.vwBusinessCashStat.domain.VwBusinessCashStat;
import com.ruoyi.project.channel.vwBusinessCashStat.service.IVwBusinessCashStatService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.freezeInfo.domain.FreezeInfo;
import com.ruoyi.project.channel.freezeInfo.service.IFreezeInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 金额冻结信息操作处理
 * 
 * @author CAIN
 * @date 2021-02-04
 */
@Controller
@RequestMapping("/channel/freezeInfo")
public class FreezeInfoController extends BaseController
{
    private String prefix = "channel/freezeInfo";
	
	@Autowired
	private IFreezeInfoService freezeInfoService;
	@Autowired
	private IVwBusinessCashStatService vwBusinessCashStatService;
	@Autowired
	private IInvestConfigService investConfigService;
	@Autowired
	private IBusinessInfoService businessInfoService;
	@Autowired
	private IBusinessCashChangeService businessCashChangeService;
	@RequiresPermissions("channel:freezeInfo:view")
	@GetMapping()
	public String freezeInfo()
	{
	    return prefix + "/freezeInfo";
	}
	
	/**
	 * 查询金额冻结列表
	 */
	@RequiresPermissions("channel:freezeInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FreezeInfo freezeInfo)
	{
		startPage();

		// 取身份信息
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if(roleKey.equals("salesman")){
			freezeInfo.setRootUserId(user.getUserId());
		}

        List<FreezeInfo> list = freezeInfoService.selectFreezeInfoList(freezeInfo);
		return getDataTable(list);
	}
	/**
	 * 查询佣金列表
	 */
	@PostMapping("/list/self")
	@ResponseBody
	public TableDataInfo listSelf(FreezeInfo freezeInfo)
	{
		freezeInfo.setUserId(ShiroUtils.getUserId());
		startPage();
		List<FreezeInfo> list = freezeInfoService.selectFreezeInfoList(freezeInfo);
		return getDataTable(list);
	}
	
	/**
	 * 导出金额冻结列表
	 */
	@RequiresPermissions("channel:freezeInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FreezeInfo freezeInfo)
    {
    	List<FreezeInfo> list = freezeInfoService.selectFreezeInfoList(freezeInfo);
        ExcelUtil<FreezeInfo> util = new ExcelUtil<FreezeInfo>(FreezeInfo.class);
        return util.exportExcel(list, "freezeInfo");
    }
	
	/**
	 * 新增金额冻结
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存金额冻结
	 */
	@RequiresPermissions("channel:freezeInfo:add")
	@Log(title = "金额冻结", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FreezeInfo freezeInfo)
	{		
		return toAjax(freezeInfoService.insertFreezeInfo(freezeInfo));
	}

	/**
	 * 新增保存金额冻结
	 */
	@Log(title = "金额冻结", businessType = BusinessType.INSERT)
	@PostMapping("/addFreeze")
	@ResponseBody
	public AjaxResult addFreeze(@RequestBody FreezeInfo freezeInfo)
	{
		User user = ShiroUtils.getSysUser();
		// 先获取当前用户账户信息
		VwBusinessCashStat stat = vwBusinessCashStatService.selectVwBusinessCashStatById(user.getUserId());
		// 如果当前可用金额 小于要冻结的金额则提示错误信息
		if(stat.getFinalCash().compareTo(freezeInfo.getAmount()) == -1){
			return AjaxResult.error("input amount error");
		}
		// 用户的可抢单天数增加
		InvestConfig investConfig = investConfigService.selectInvestConfigById(freezeInfo.getInvestId());
		BusinessInfo businessInfo = businessInfoService.selectBusinessInfoById(user.getUserId());
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(businessInfo.getDueDate());
		calendar1.add(Calendar.DATE, investConfig.getEffectiveDay());
		businessInfo.setDueDate(calendar1.getTime());
		businessInfoService.updateBusinessInfo(businessInfo);
		freezeInfo.setStartTime(new Date());
		freezeInfo.setRealAmount(freezeInfo.getAmount());
		BigDecimal fee= freezeInfo.getAmount().multiply(investConfig.getRate());
		freezeInfo.setAmount(freezeInfo.getAmount().add(fee));
		freezeInfo.setFreezeDay(investConfig.getEffectiveDay());
		calendar1.setTime(new Date());
		calendar1.add(Calendar.DATE, investConfig.getEffectiveDay());
		freezeInfo.setEndTime(calendar1.getTime());
		freezeInfo.setUserId(user.getUserId());
		businessCashChangeService.insertBusinessCashChange(fee,freezeInfo.getUserId(), "7", "优惠奖励");
		freezeInfoService.insertFreezeInfo(freezeInfo);
		return AjaxResult.success("success");
	}


	/**
	 * 修改金额冻结
	 */
	@GetMapping("/edit/{freezeId}")
	public String edit(@PathVariable("freezeId") Long freezeId, ModelMap mmap)
	{
		FreezeInfo freezeInfo = freezeInfoService.selectFreezeInfoById(freezeId);
		mmap.put("freezeInfo", freezeInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存金额冻结
	 */
	@RequiresPermissions("channel:freezeInfo:edit")
	@Log(title = "金额冻结", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FreezeInfo freezeInfo)
	{		
		return toAjax(freezeInfoService.updateFreezeInfo(freezeInfo));
	}
	
	/**
	 * 删除金额冻结
	 */
	@RequiresPermissions("channel:freezeInfo:remove")
	@Log(title = "金额冻结", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(freezeInfoService.deleteFreezeInfoByIds(ids));
	}
	
}
